package com.jck.data.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.beanutils.PropertyUtils;


public class ChkUtil {
	
	public static final String EXP_a_z = "[a-z]*";
	public static final String EXP_A_Z = "[A-Z]*";
	public static final String EXP_a_z_A_Z = "[a-zA-Z]*";
	public static final String EXP_0_9 = "[0-9]*";
	public static final String EXP_0_9_a_z = "[0-9a-z]*";
	public static final String EXP_0_9_a_z_A_Z = "[0-9a-zA-Z]*";
	public static final String EXP_0_9_a_z__ = "[0-9a-z_]*";
	public static final String EXP_EMAIL = "^([a-z0-9A-Z_]+[_|\\-|\\.]?)+[a-z0-9A-Z_]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
	public static final String EXP_PRICE = "^([1-9]\\d+|[1-9])(\\.\\d\\d?)*$";
	public static final String EXP_MOBILE = "[0-9]{11}";
	public static final String EXP_POSTALCODE = "[0-9]{6}";
	public static final String EXP_TEL = "[0-9]{3,4}[-]{1}[0-9]{7,8}";
	public static final String EXP_IP = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
	public static final String EXP_CHINESE = "[һ-�]*";
	public static final String EXP_0_9_a_z_A_Z_CHINESE = "[0-9a-zA-Zһ-�]*";
	public static final String EXP_URL = "^[a-zA-z]+://[^><\"' ]+";
	public static final String EXP_DATE = "[0-9]{4}[-]{1}[0-9]{1,2}[-]{1}[0-9]{1,2}";
	public static final String EXP_DATETIME = "[0-9]{4}[-]{1}[0-9]{1,2}[-]{1}[0-9]{1,2}[ ]{1}[0-9]{1,2}[:]{1}[0-9]{1,2}";
	public static final String EXP_DATETIMESECOND = "[0-9]{4}[-]{1}[0-9]{1,2}[-]{1}[0-9]{1,2}[ ]{1}[0-9]{1,2}[:]{1}[0-9]{1,2}[:]{1}[0-9]{1,2}";
	public static final String DATESTRING_TAIL = "000000000";
	public static final String EXP_DATETIMESECOND2 = "[0-9]{4}[-]{1}[0-9]{1,2}[-]{1}[0-9]{1,2}[ ]{1}[0-9]{1,2}[:]{1}[0-9]{1,2}[:]{1}[0-9]{1,2}[.]{1}[0-9]{1,3}";
	public static final String FILED_NAME = "name";
	public static final String FILED_LENGTH = "length";
	public static final String FILED_TYPE = "type";
	public static final String FILED_ISUNIQUE = "isUnique";
	public static final String FILED_ISCHECKEX = "isCheckEx";
	public static final String FILED_HINT = "hint";
	public static final String EXP_SQL_INJECT_ONE = "(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|"
			+ "(\\b(select|update|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)";

	public static boolean isEmpty(Object data) {
		return (data == null) || ("".equals(data.toString().trim()));
	}
	public static boolean isEmptyAllObject(Object data) {
		if((data == null) || ("".equals(data.toString().trim()))){
			return (data == null) || ("".equals(data.toString().trim()));
		}else if(data instanceof Map){
			Map map=(Map) data;
			return (data == null) ||( map.size()==0);
		}else if(data instanceof Collection){
			Collection cc=(Collection) data;
			return (data == null) ||( cc.size()==0);
		}else{
			return (data == null) || ("".equals(data.toString().trim()));
		}
			
		
	}

	public static boolean isStr(String str) {
		return !isEmpty(str);
	}

	public static boolean isStr(String str, String patternStr) {
		if (isEmpty(str)) {
			return false;
		}
		Pattern pattern = Pattern.compile(patternStr);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}


	public static boolean isMoney(String str) {
		if (isEmpty(str))
			return false;
		try {
			Double.valueOf(str);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	public static boolean isInteger(Object str) {
		if (isEmpty(str))
			return false;
		try {
			Integer.valueOf((String)str);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	public static boolean isLong(String str) {
		if (isEmpty(str))
			return false;
		try {
			Long.valueOf(str);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	public static boolean isDouble(String str) {
		if (isEmpty(str))
			return false;
		try {
			Double.valueOf(str);
			return true;
		} catch (Exception e) {
		}
		return false;
	}
	
	public static boolean isBigDecimal(Object str) {
		if (isEmpty(str))
			return false;
		try {
			new BigDecimal(str+"");
			return true;
		} catch (Exception e) {
		}
		return false;
	}
	
	public static boolean isBigDecimal(BigDecimal str) {
		if (isEmpty(str))
			return false;
		try {
			new BigDecimal(str+"");
			return true;
		} catch (Exception e) {
		}
		return false;
	}
	
	public static boolean isDate(Object str) {
		if (isEmpty(str))
			return false;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			sdf.parse((String)str);
			return true;
		} catch (Exception e) {
		}
		return false;
	}
	
	public static boolean isDate(Date str) {
		if (isEmpty(str))
			return false;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			sdf.format(str);
			return true;
		} catch (Exception e) {
		}
		return false;
	}
	
	public static boolean isYear(Object str) {
		if (isEmpty(str))
			return false;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			sdf.parse((String)str);
			return true;
		} catch (Exception e) {
		}
		return false;
	}
	
	/**
	 * 
	 * SQL参数注入 
	 * 修改内容 
	 * @param regExps 校验数组
	 * @return   
	 * @throws 
	 */
	public static boolean chkParamSqlInject(String... regExps) {
		if(isEmpty(regExps))
			return true;
		
		try {
			for (String string : regExps) {
				if (!chkParamSqlInject(string))
					return false;
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	/**
	 * 
	 * SQL参数注入
	 * 修改内容 
	 * @param 
	 * @return   
	 * @throws 
	 */
	public static boolean chkParamSqlInject(String regExp) throws UnsupportedEncodingException {
		if(ChkUtil.isEmpty(regExp))
			return true;
		
		Pattern sqlPattern = Pattern.compile(EXP_SQL_INJECT_ONE,Pattern.CASE_INSENSITIVE);
		String param = URLDecoder.decode(regExp,"UTF-8");
		if (sqlPattern.matcher(param).find())
			return false;
		
		return true;
	}
	
	/**
	 * 判断字符串中类型
	* @Description: TODO(描述) 
	* @param  str  数据
	* @author 作者 jacking 
	* @date 2016-7-21 下午4:26:55 
	* @return
	 */
	public static Set getDataTypeSet(Object str) {
		Set<String> data = new HashSet<>();
			   boolean flag=false;
		/*	   if(isEmpty(str)){
				   data.add("java.util.Date");
				   data.add("java.math.BigDecimal");
				   data.add("java.lang.Integer");
				   data.add("java.lang.Long");
				   data.add("int");
				   data.add("long");
				   data.add("java.lang.Double");
				   data.add("double");
				   data.add("java.lang.Float");
				   data.add("float");
				   data.add("java.lang.String");
				   return data;
			   }*/
			   if(str.getClass().getName().equals("java.util.Date")){
				   data.add("java.util.Date");
				   return data;
			   }
			   if ((str+"").matches("\\d*|\\d+(\\.\\d{1,10})")) {
				   data.add("java.math.BigDecimal");
				   data.add("java.lang.String");
				   flag=true;
			   }
			   
			   if((str+"").matches("\\d*")){
				   data.add("java.lang.Integer");
				   data.add("java.lang.Long");
				   data.add("int");
				   data.add("long");
			   }
			   if((str+"").matches("\\d+(\\.\\d{1,10})")){
				   data.add("java.lang.Double");
				   data.add("double");
				   data.add("java.lang.Float");
				   data.add("float");
			   }
			   
			   if(!flag){
				   data.add("java.lang.String");
			   }
			   
		return data;
	}
	
 
	/**
	 * 比较数据是否符合所指定长度
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @param str
	* @param @param oldLength
	* @param @return    设定
	 */
	public static boolean isOKLength(Object str,int oldLength) {
		if(!str.getClass().getName().equals("java.util.Date")){
			if((str+"").length()>oldLength){
				return false;
			}
		}
		 
		return true;
	}
	
	
	/**
	 * 
	* @Description: TODO(描述) 
	* @param  str
	* @param  regexStr
	* @author 作者 jacking 
	* @date 2016-8-1 上午11:25:23 
	* @return
	 */
	public static boolean chekRegex(Object str, String regexStr) {
		if (isEmpty(str)||isEmpty(regexStr)) {
			return true;
		}
		if(!str.getClass().getName().equals("java.util.Date")){
			Pattern pattern = Pattern.compile(regexStr);
			Matcher matcher = pattern.matcher(str+"");
			return matcher.matches();
		}
		return true;
	}
		
	
	/**
	 * String 替换XSS注入关键字
	* @Description: TODO(描述) 
	* @param  设定
	* @author 作者 jacking 
	* @date 2016-8-3 上午11:20:55 
	* @return
	 */
	public static String getXSSEscapeByString(String v) throws UnsupportedEncodingException {
		if(ChkUtil.isEmpty(v))
			return v;
		      v=v.replaceAll("&", "&amp;")
				.replaceAll("\"", "&quot;")
				.replaceAll("'", "&#39;")
				.replaceAll("<", "&lt;")
				.replaceAll(">", "&lt;");
		return v;
	}
	/**
	 * Object 替换XSS注入关键字
	 * @Description: TODO(描述) 
	 * @param  设定
	 * @author 作者 jacking 
	 * @date 2016-8-3 上午11:20:55 
	 * @return
	 */
	public static Object  getXSSEscapeByObject(Object o,Class clz) throws UnsupportedEncodingException {
		java.lang.reflect.Field[] flds=null;
		if(clz.getSimpleName().equals("Map")){
			Map map;
			try {
				map = (Map) o;
				Set<String> keySet = map.keySet();
				for (String string : keySet) {
					Object value = map.get(string);
					if(value!=null&&value.getClass().getSimpleName().equals("String")){
						String  v=value+"";
						v=getXSSEscapeByString(v);
						map.put(string, v);
					}
					
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return o;
		}
	    flds = clz.getDeclaredFields(); 
		for(java.lang.reflect.Field  fld:flds){
			if(fld.getType().getSimpleName().equals("String")){
				String mn=fld.getName();
				Object obj;
				try {
					obj= PropertyUtils.getProperty(o, mn);
					if(null!=obj){
						String  v=obj+"";
						v=getXSSEscapeByString(v);
						org.apache.commons.beanutils.BeanUtils.copyProperty(o, mn, v);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return o;
		
	}
 

}
